package leecode

func dailyTemperatures(temperatures []int) []int {
	result := make([]int, len(temperatures))
	stack := make([]int, 0)
	stack = append(stack, 0) //单调栈,存放之前遍历过的元素下标
	for i := 1; i < len(temperatures); i++ {
		//栈顶的下标
		topIndex := stack[len(stack)-1]
		//记录结果 , 将栈顶元素弹出
		if temperatures[i] > temperatures[topIndex] {
			for len(stack) != 0 && temperatures[i] > temperatures[topIndex] {
				result[topIndex] = i - topIndex
				stack = stack[:len(stack)-1]
				if len(stack) != 0 {
					topIndex = stack[len(stack)-1]
				}
			}
			stack = append(stack, i) //将当前元素加入栈
		} else if temperatures[i] < temperatures[topIndex] {
			stack = append(stack, i)
		} else {
			stack = append(stack, i)
		}
	}
	return result
}
